// pages/index/index.js
var api = require('../../utils/api.js');
var sliderWidth = 96;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["简约", "中式", "欧式","美式"],
    activeIndex: 0,
    loupanlist:[],//楼盘列表
    gongyilist:[],
    anlilist:[],
    adviserlist:[],
    after_service:[],
    stylecases:[[], [], [], []],
    isshow:false,
    citys: [],
    index:-1,
    intdata:''
  },

  loadswiper(){
    api.get({
      url: 'home/slides/1',
      success: data => {
        console.log(data)
        if (data.code == 1) {
          this.setData({
            movies: data.data[0].items
          })
        }
      }
    });
  },
  loadcailiao() {
    api.get({
      url: 'home/slides/41',
      success: data => {
        if (data.code == 1) {
          this.setData({
            cailiao: data.data[0].items
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    this.loadswiper();
    this.loadloupan();
    this.loadcases();
    this.loadfuwu();
    this.load3dcase();
    this.loadafterservice();
    this.loadadviser();
    this.loadgongyi();
    this.onlineform = this.selectComponent("#onlineform");
    api.getparams();
    this.loadcailiao();
  },
  tabClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },
  loadfuwu() {
    api.get({
      url: 'home/slides/3',
      success: data => {
        if (data.code == 1) {
          this.setData({
            fuwulist: data.data[0].items
          })
        }
      }
    });
  },
 
  loadafterservice() {
    api.get({
      url: 'home/slides/4',
      success: data => {
        if (data.code == 1) {
          this.setData({
            after_service: data.data[0].items
          })
        }
      }
    });
  },
//加载3d实景图
  load3dcase(){
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 4,
        field: 'post.id,post.post_title,post.more',
        is_top: 1,
        limit:1,
        order: '-post.id'
      },
      success: data => {
        console.log(data.data)
        if (data.code == '1') {
          this.setData({
            case3d: data.data.list[0]
          })
        }
      }
    })
  },

  //加载工地工艺
  loadgongyi() {
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 8,
        field: 'post.id,post.post_title,post.more',
        is_top: 1,
        order: '-post.id'
      },
      success: data => {
        if (data.code == '1') {
          this.setData({
            gongyilist: data.data.list
          })
        }
      }
    })
  },

  formSubmit(e) {
    //提交表单数据
    console.log(e)
    api.post({
      url: '/portal/diyform/insert2',
      data: e.detail.value,
      success: data => {
        if (data.code == "1") {
          wx.showToast({ title: '报名成功',icon:'success'})
        }
        if (data.code == "0") {
          wx.showToast({ title: data.msg, icon: 'none' })
        }
      }
    })
  },

  openswith(){
    wx.switchTab({
      url: '/pages/about/index',
    })
  },
  onTabItemTap(e) {
    console.log(e)
    if (e.index == 2) {
      var params = wx.getStorageSync('params');
      wx.makePhoneCall({
        phoneNumber: params['tel']
      })
    }
  },
  previewimage(e) {
    var imgurl = e.currentTarget.dataset.url
    wx.previewImage({
      current: imgurl, // 当前显示图片的http链接
      urls: [imgurl] // 需要预览的图片http链接列表
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  //加载热装楼盘
  loadloupan(){
    api.get({
      url:'/portal/lists/getCategoryPostLists',
      data:{
        category_id:1,
        field:'post.id,post.post_title,post.more',
        is_top:1,
        order:'-post.id'
      },
      success:data=>{
        if(data.code=='1'){
          this.setData({
            loupanlist:data.data.list
          })
        }
      }
    })
  },
  //加载家装顾问
  loadadviser(){
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 7,
        field: 'post.id,post.post_title,post.more',
        is_top: 1,
        order: '-post.id'
      },
      success: data => {
        if (data.code == '1') {
          this.setData({
            adviserlist: data.data.list
          })
        }
      }
    })
  },
  //加载家装案例
  loadcases(){
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 2,
        field: 'post.id,post.post_title,post.more',
        is_top: 1,
        limit:12,
        order: '+list_order'
      },
      success: data => {
        if (data.code == '1') {
          let stylecases = this.data.stylecases;
          let arr=data.data.list;
          for (var i = 0; i < arr.length;i++){
            stylecases[arr[i].more.style].push(arr[i])
          }
         
          this.setData({
            stylecases: stylecases
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})